package com.ccccat.www.msgs.Receiver;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.*;
import android.content.*;
import android.content.pm.*;

import com.ccccat.www.msgs.MainActivity;
import com.ccccat.www.msgs.service.WakeLockService;

public class PhoneReceiver extends BroadcastReceiver{
     private static String TAG="PhoneReceiver";
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){			
			//监听开机启动
			
			Toast.makeText(context,"开机了",Toast.LENGTH_LONG).show();


			final ComponentName receiver = new ComponentName(context,MainActivity.class);
			final PackageManager pm = context.getPackageManager(); 
			pm.setComponentEnabledSetting(receiver,PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,PackageManager.DONT_KILL_APP);
			
			
			
			Intent service=new Intent(context,WakeLockService.class);
			//启动服务
			context.startService(service);
			Toast.makeText(context,"开机启动哈！",Toast.LENGTH_LONG).show();
			Intent i=new Intent();
			i.setClass(context, MainActivity.class);
			i.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(i);
			
			Log.i(TAG, "开机监听启动服务！!");
		}else if(intent.getAction().equals("android.intent.relodingservice")){
	        //服务被销毁后再次启动服务
			Intent service=new Intent(context,WakeLockService.class);
			//启动服务
			context.startService(service);
			Toast.makeText(context,"我再启动哈！",Toast.LENGTH_LONG).show();
			
			Log.i(TAG, "ondestroy重新启动服务！!");
		}else if(intent.getAction().equals("android.intent.restartservice")){
			 //服务被销毁后再次启动服务
			Intent service=new Intent(context,WakeLockService.class);
			//启动服务
			context.startService(service);
			Toast.makeText(context,"我再启动哈！",Toast.LENGTH_LONG).show();
			
			Log.i(TAG, "stopservice重新启动服务");
		}
		
	}

}
